package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ReceiveListAdapter
import com.ht.medicaltreatment.bean.YangBenJieShouBeanRfidSample
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.SoundUtils
import com.ht.medicaltreatment.viewmodel.SampleReceiveListViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_receive_list_layout.btnNext
import kotlinx.android.synthetic.main.activity_receive_list_layout.btnStart
import kotlinx.android.synthetic.main.activity_receive_list_layout.recyclerView
import kotlinx.android.synthetic.main.activity_receive_list_layout.titleJ
import kotlinx.android.synthetic.main.activity_receive_list_layout.tvCount

class SampleReceiveListActivity : BaseActivity(), View.OnClickListener {
    private val model by viewModels<SampleReceiveListViewModel>()
    private val uidList = arrayListOf<String>()
    private lateinit var adapter: ReceiveListAdapter
    private var isStanding = false
    var list = arrayListOf<YangBenJieShouBeanRfidSample>()
    var addressList:ArrayList<String> = arrayListOf()
    private var isStartScan = false

    private var deliveryCondition = ""
    private var drawBloodPlace = ""
    private var receivingPlace = ""
    private var bloodSource = ""

    override fun getLayoutId() = R.layout.activity_receive_list_layout

    override fun init() {
        setTitle("样本接收")
        ScanUtils.init(onInitResult = {
            if(it){
                btnStart.setBackgroundColor(getColor(R.color.blue))
                btnStart.isEnabled = true
            }else{
                btnStart.text = "无法打开"
            }
        })
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = ReceiveListAdapter(isStanding)
        btnStart.setOnClickListener(this)
        btnNext.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when(v){
            //下一步
            btnNext -> {
                if(list.size == 0){
                    showToast("请扫描样本")
                    return
                }
                val intent = Intent(this,SampleReceiveBuildActivity::class.java)
                intent.putParcelableArrayListExtra("list",list)
                intent.putStringArrayListExtra("addressList",addressList)
                intent.putExtra("isStanding",isStanding)
                intent.putExtra("deliveryCondition",deliveryCondition)
                intent.putExtra("drawBloodPlace",drawBloodPlace)
                intent.putExtra("receivingPlace",receivingPlace)
                intent.putExtra("bloodSource",bloodSource)
                startActivity(intent)
                reset()
            }
            // 开始/停止扫描
            btnStart -> {
                if(!isStartScan){
                    //开始识别
                    reset()
                    isStartScan = true
                    btnStart.setText("停止扫描")
                    btnStart.setBackgroundColor(getColor(R.color.red))
                    uidList.clear()
                    ScanUtils.open {
                        SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
                        uidList += it
                        tvCount.text = "${uidList.size}"
                    }
                }else{
                    //停止识别
                    isStartScan = false
                    btnStart.setText("开始扫描")
                    btnStart.setBackgroundColor(getColor(R.color.blue))
                    list.clear()
                    ScanUtils.close()
                    if(uidList.size == 0)return
                    showProgress()
                    try {
                        model.queryName(uidList,{
                            dismissProgress()
                            addressList?.clear()
                            if(it.ReSample.isNotEmpty()){
                                showToast("已有表单包含次样本\n${it.ReSample}")
                                return@queryName
                            }

                            if(it.emptyTagCount > 0){
                                showToast("包含空标签，空标签数量: ${it.emptyTagCount}")
                                return@queryName
                            }

                            if(!it.sameProject){
                                showToast("扫描到的样本不在同一项目中!")
                                return@queryName
                            }
                            deliveryCondition = it.deliveryCondition
                            drawBloodPlace = it.drawBloodPlace
                            receivingPlace = it.receivingPlace
                            bloodSource = it.bloodSource
                            isStanding = it.isOrNotStandingTime == 1
                            if(isStanding){
                                titleJ.visibility = View.VISIBLE
                            }
                            adapter = ReceiveListAdapter(isStanding)
                            recyclerView.adapter = adapter
                            list.addAll(it.rfidSampleList)
                            //list = it.rfidSampleList as ArrayList<YangBenJieShouBeanRfidSample>?
                            it.bloodRoomList.forEach {
                                addressList.add(it.name)
                            }
                            showToast("识别到${it.rfidSampleList.size}条数据")
                            adapter.setNewInstance(it.rfidSampleList)
                        },{
                            dismissProgress()
                            showToast(it)
                        })
                    }catch (e:Exception){
                        LogUtils.i(e.message)
                    }
                }
            }
        }
    }

    fun reset(){
        adapter.setNewInstance(null)
        uidList.clear()
        list.clear()
        tvCount.text = "0"
    }

    override fun onDestroy() {
        super.onDestroy()
        ScanUtils.onDestroy()
    }

}
